package com.asia5b.a5mall.goods.adapter

import androidx.recyclerview.widget.RecyclerView
import com.asia5b.a5bmall.resource.ResString
import com.asia5b.a5mall.goods.R
import com.asia5b.a5mall.middleware.model.goods.Goods2SpecModel
import com.asia5b.a5mall.middleware.utils.dddLog
import com.asia5b.a5mall.middleware.utils.showMessage
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.android.flexbox.FlexboxLayoutManager

/**
 * Created by zhangyuncai on 2020/1/18.
 */
class SkuAdapter(list: ArrayList<ArrayList<Goods2SpecModel>>) :

        BaseQuickAdapter<ArrayList<Goods2SpecModel>, BaseViewHolder>(R.layout.item_sku_value, list) {

    var callback: SkuCallBack? = null;

    override fun convert(helper: BaseViewHolder, item: ArrayList<Goods2SpecModel>) {
        val layoutPosition = helper.layoutPosition
        helper.setText(R.id.tv_sku_name, item[0].spec_name)
        val recyclerView = helper.getView<RecyclerView>(R.id.rv_sku_value)
        val specAdapter = SpecAdapter(item)
        recyclerView.run {
            layoutManager = FlexboxLayoutManager(context)
            adapter = specAdapter
        }
        specAdapter.setOnItemChildClickListener { adapter, view, position ->
            val goods2SpecModel = specAdapter.getItem(position) as Goods2SpecModel
            if (goods2SpecModel.bgState == 1) {//已经被选中了,点击无效
                dddLog("已经被选中了,点击无效")
            } else if (goods2SpecModel.bgState == 0) {//未被选中,并且可以点击
                callback?.callback(layoutPosition, goods2SpecModel.spec_value_id)
            } else {//表示不能点击
                dddLog("不能点击")
                showMessage(ResString.get(R.string.无库存))
            }
        }
    }

}